// Dom7
var $$ = Dom7;

// Framework7 App main instance
var app = new Framework7({
	root: '#app', // App root element
	id: 'io.yachtar.app', // App bundle ID
  	name: 'Yachtar', // App name
	theme: 'auto', // Automatic theme detection
	dialog: {
		// set default title for all dialog shortcuts
		title: '提示',
		// change default "OK" button text
		buttonOk: '确认',
	  }
});

// Init/Create main view
var mainView = app.views.create('.view-main', {
	url: '/'
});

var pageUrl = window.location.href;
var m = pageUrl.lastIndexOf('/');
var n = pageUrl.lastIndexOf('.');
var str = pageUrl.substring(m+1,n);
var exitAppTicker = 0;
document.addEventListener("deviceready",function(){
    document.addEventListener("backbutton", function(){
       if(str == "index"){
            if(exitAppTicker == 0){
                    setTimeout(function(){
                            exitAppTicker = 0;
                    },2000);
                    navigator.app.exitApp(); //退出app
            }
        }else{
            history.back();
        }
    }, false);
},false);

$(document).ready(function(){
	$("#loginname").val(sessionStorage.getItem("loginname"));
});
//登陆
$$('#login-button').on('click', function () {
	var loginname = $("#loginname").val();
	var password = $("#password").val();
	//sessionStorage.setItem("loginname",loginname);
	//window.location.href = "plugin/homepage/homepage.html";
	if (loginname == "") {
		app.dialog.alert("请先填写用户名！");
		return;
	}
	if (loginname.length > 128) {
		app.dialog.alert("用户名超出长度限制！");
		return;
	}
	var params = { loginname: loginname, password: password };
	$.ajax({
		type: "POST",
		url: "http://61.160.250.164:8088/bfmis/applogin.yachtar",
		data: JSON.stringify(params),
		dataType: "json",
		contentType: "application/json;charset=UTF-8",
		success: function (data) {
			if (data.hasOwnProperty("error_content")) {
				app.dialog.alert("用户名不存在！");
			} else if(data.ispassword=="N"){
				app.dialog.alert("密码错误！");
			} else if(data.rightname==null){
				app.dialog.alert("用户权限不足！");
			}else if(data.isactive=="N"){
				app.dialog.alert("该账户已停用！请联系管理员！");
			}else if(data.isactive=="O"){
				app.dialog.alert("您之前注册的单位正在审核中！请等待审核通过！");
			}else {
				window.location.href = "plugin/homepage/homepage.html";
				sessionStorage.setItem("loginname",loginname);
				sessionStorage.setItem("userid",data.userid);
			}
		}
	});
});

